package hc.server.ui.design.hpj;

import hc.App;
import hc.res.ImageSrc;
import hc.server.ConfigPane;
import hc.server.FileSelector;
import hc.server.ui.design.Designer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

public class MenuManager {
	private static int newNodeIdx = 1;
	
	public static int getNextNodeIdx(){
		return newNodeIdx++;
	}
	public void updateSkinUI(){
		Iterator<JPopupMenu> it = map.values().iterator();
		while(it.hasNext()){
			ConfigPane.updateComponentUI(it.next());
		}
	}
	
	private Map<Integer, JPopupMenu> map = new HashMap<Integer, JPopupMenu>(); 
	
	public void popUpMenu(int type, TreeNode treeNode, JTree tree, MouseEvent e, final Designer designer){
		final Integer integer_type = Integer.valueOf(type);
		JPopupMenu pMenu = map.get(integer_type);
		
		//Menu关联菜单尚未创建
		if(pMenu == null){
			if(HPNode.isNodeType(type, HPNode.MASK_ROOT)){
				pMenu = new JPopupMenu();
				JMenuItem addItem = new JMenuItem("new menu");
				addItem.setIcon(Designer.iconMenu);
				addItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new HPMenu(
								HPNode.MASK_MENU,
								"new node " + getNextNodeIdx()));
						
						HCjar.buildMenuEventNodes(new HashMap<String, Object>(), newNode, 0);
						
						designer.addNode(newNode);
					}
				});
				pMenu.add(addItem);
				
				map.put(integer_type, pMenu);
			}else if(HPNode.isNodeType(type, HPNode.MASK_MENU)){
				pMenu = new JPopupMenu();
				JMenuItem addItem = new JMenuItem("new menu item");
				addItem.setIcon(Designer.iconMenuItem);
				addItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						//超过菜单最大数
						final int currMenuItemNum = designer.getCurrSelectedNode().getChildCount();
						if(currMenuItemNum >= MenuListEditPanel.MAX_MENUITEM){
							JPanel panel = new JPanel();
							panel.add(new JLabel("Curr menu item number > " + MenuListEditPanel.MAX_MENUITEM, 
									App.getSysIcon(App.SYS_ERROR_ICON), SwingConstants.LEFT));
							App.showCenterPanel(panel, 0, 0, "Too much items!", false, null, null, null, designer, true, false);
							return;
						}
						
						DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
								new HPMenuItem(
								HPNode.TYPE_MENU_ITEM_CMD,
								"new node " + getNextNodeIdx()));
						designer.addNode(newNode);
					}
				});
				pMenu.add(addItem);
				
				JMenuItem delItem = new JMenuItem("del menu");
				delItem.setIcon(Designer.iconDel);
				delItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						designer.delNode();
					}
				});
				pMenu.add(delItem);
				
				map.put(integer_type, pMenu);
			}else if(HPNode.isNodeType(type, HPNode.MASK_MENU_ITEM)){
				pMenu = new JPopupMenu();
				
				JMenuItem delItem = new JMenuItem("del menu item");
				delItem.setIcon(Designer.iconDel);
				delItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						designer.delNode();
					}
				});
				pMenu.add(delItem);
				
				map.put(integer_type, pMenu);
			}else if(HPNode.isNodeType(type, HPNode.MASK_SHARE_RB)){
				pMenu = new JPopupMenu();
				
				JMenuItem delItem = new JMenuItem("del JRuby file");
				delItem.setIcon(Designer.iconDel);
				delItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						designer.delNode();
					}
				});
				pMenu.add(delItem);
				
				map.put(integer_type, pMenu);
			}else if(HPNode.isNodeType(type, HPNode.MASK_SHARE_RB_FOLDER)){
				pMenu = new JPopupMenu();
				JMenuItem addItem = new JMenuItem("new JRuby share file");
				addItem.setIcon(Designer.iconShareRB);
				addItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
								new HPShareJRuby(
								HPNode.MASK_SHARE_RB,
								"share" + getNextNodeIdx() + ".rb"));
						designer.addNode(newNode);
					}
				});
				pMenu.add(addItem);
				
				map.put(integer_type, pMenu);
			}else if(HPNode.isNodeType(type, HPNode.MASK_RESOURCE_FOLDER)){
				pMenu = new JPopupMenu();
				JMenuItem addItem = new JMenuItem("new jar file");
				addItem.setIcon(Designer.iconJar);
				addItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						File addJarFile = FileSelector.selectImageFile(designer, FileSelector.JAR_FILTER, true);
						if(addJarFile != null){
							DefaultMutableTreeNode newNode;
							try {
								newNode = new DefaultMutableTreeNode(
										new HPShareJar(
										HPNode.MASK_RESOURCE_JAR,
										addJarFile.getName(), addJarFile));
								designer.addNode(newNode);
							} catch (Exception e) {
								JPanel ok = new JPanel();
								try {
									ok.add(new JLabel("Error load jar file!", new ImageIcon(ImageIO.read(ImageSrc.CANCEL_ICON)), SwingConstants.LEFT));
								} catch (IOException e2) {
								}
								App.showCenterPanel(ok, 0, 0, "Load Error!", false, null, null, null, designer, true, false);
								
								e.printStackTrace();
							}
						}
					}
				});
				pMenu.add(addItem);
				
				map.put(integer_type, pMenu);
			}else if(type == HPNode.MASK_RESOURCE_JAR){
				pMenu = new JPopupMenu();
				{
					JMenuItem delItem = new JMenuItem("save as ...");
					delItem.setIcon(Designer.iconSaveAs);
					delItem.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent event) {
							designer.saveAs();
						}
					});
					pMenu.add(delItem);					
				}
				
				{
					JMenuItem delItem = new JMenuItem("del jar file");
					delItem.setIcon(Designer.iconDel);
					delItem.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent event) {
							designer.delNode();
						}
					});
					pMenu.add(delItem);
				}
				map.put(integer_type, pMenu);
			}
		}
		
		if(HPNode.isNodeType(type, HPNode.MASK_ROOT)){
			//限制只能加一个Menu
			if(treeNode.getChildCount() > 1){
				return;
			}
		}
		if(pMenu != null){
			pMenu.show(e.getComponent(), e.getX(), e.getY());
		}
	}
}
